/**
 * 脚本函数
 * @param executeCommand
 */
function sScriptFuncs(executeCommand){
    /**
     * 函数列表
     */
    var list = [
        {
            label: "ID",
            param: "(\"填入ID\")",
            name: "getById",
            type: "common|find",
            description: `
            功能：根据元素ID获取元素引用。<br/>
            用法：设置ID等于a1元素的宽度，
            ID(\"a1\").style.width="100px";
            `
        },
        {
            label: "NAME",
            param: "(\"填入名称\")",
            name: "getByName",
            type: "common|find",
            description: `
            功能：根据元素名称获取元素引用，由于名称允许重复，所以会返回一个元素引用的数组。<br/>
            用法：设置名称等于a1元素的宽度，<br/>
            NAME(\"a1\")[0].style.width="100px";
            如果要设置多个，可以使用for循环遍历。
            `
        },
        {
            label: "SELF",
            param: "()",
            name: "getSelf",
            type: "common|find",
            description: `
            功能：获取当前元素引用。<br/>
            用法：SELF()
            `
        },
        {
            label: "DISPLAY",
            param: "({id: \"1\", checked: true})",
            name: "subscribe",
            type: "common|expression",
            description: `
            功能：设置当前元素是否可见。<br/>
            用法：DISPLAY({id: "1", checked: true}), 
            表示id="1"的元素的属性checked=true时，当前元素可见。
            `
        },
        {
            label: "COMMAND",
            param: "(\"命令名称\", 命令参数)",
            name: "execCommand",
            type: "common|find",
            description: `
            功能：对当前文档执行命令，具体命令的类型和参数见<a href="#">命令说明</a>。<br/>
            用法：COMMAND("insertText", "hello world")
            `
        },
        {
            label: "if",
            param: `(条件){\r\n}`,
            type: "condition",
            description: `
            功能：条件函数。<br/>
            用法：
            if(GET_ID("a1").value == "1"){<br/>
                GET_ID("a2").style.display = "none";<br/>
            }
            `
        },
    ]

    /**
     * 获取所有的函数列表
     * @returns 
     */
    this.getAllFuns = function(){
        return list;
    }

    var minPageIndex = 0;
    var minRowIndex = 0;

    /**
     * 获取当前元素
     * @returns 
     */
    var getSelf = function(){
        return executeCommand("getCurrentElement");
    }

    /**
     * 根据ID获取元素
     * @param {*} id 
     * @returns 
     */
    var getById = function(id){
        let el = executeCommand("querySelector", "#" + id);
        setMinIndex(el);
        return el.ref;
    }

    /**
     * 根据名称获取元素
     * @param {*} name 
     * @returns 
     */
    var getByName = function(name){
        let refs = [];
        let els = executeCommand("querySelector", "[name='" + name + "']");
        
        for(let i in els){
            refs.push(els[i].ref);
            setMinIndex(els[i]);
        }

        return refs;
    }

    /**
     * 订阅
     * @param {*} param 
     */
    var subscribe = function(param){
        
    }

    /**
     * 执行命令
     */
    var execCommand = function(name, value){
        return executeCommand(name, value);
    }

    /**
     * 设置脚本涉及到文档的最小页和行的序号
     * @param {*} el 
     */
    var setMinIndex = function(el){
        if(!minPageIndex || minPageIndex < el.pageIndex){
            minPageIndex = el.pageIndex;
            minRowIndex = el.rowIndex;
        }

        if(minPageIndex == el.pageIndex && minRowIndex < el.rowIndex){
            minRowIndex = el.rowIndex;
        }

        if(!el.ref) return;

        let ref = el.ref;

        if(ref.__endTag && ref.__endTag.__boundRect){
            let pageIndex = ref.__endTag.__boundRect.length - 1;
            let rowIndex = ref.__endTag.__boundRect[pageIndex].length - 1;

            setMinIndex({
                pageIndex: pageIndex,
                rowIndex: rowIndex
            });
        }
    }

    /**
     * 获取脚本涉及到文档的最小页和行的序号
     */
    this.getMinIndex = function(){
        let min = {
            pageIndex: minPageIndex,
            rowIndex: minRowIndex
        }

        minPageIndex = 0;
        minRowIndex = 0;

        return min;
    }

    /**
     * 执行脚本
     * @param {*} script 
     * @param {*} option
     */
    this.run = function(script, option){
        script = replaceFunInScript(script);
        
        let js = `
            (function test(){
                try{
                    ${script}
                }catch(e){
                    return e;
                }
            })();
        `;

        return eval(js);
    }

    /**
     * 替换脚本中的函数名
     * @param {*} script 
     */
    var replaceFunInScript = function(script){
        var reg = null;

        for(let i in list){
            if(!list[i].name) continue;

            reg = new RegExp(list[i].label, "gi");
            script = script.replace(reg, list[i].name);
        }

        return script;
    }
}